import http from '../../../common/httpUtil';
import * as types from './fundPortfolioMutationTypes';


const actions = {
  queryPortfolio({commit}, {combinationCode} = {}) {
    return http.post(`portfolio/query/getOne?combinationCode=${combinationCode}`).then(Portfolio => {
      console.log(Portfolio)
      return Portfolio;
    })
  },

  queryCombination({commit}, {combinationCode} = {}) {
    commit(types.COMBINATION_QUERY_BEGIN);
    return http.post(`portfolio/query/get_mapping?combinationCode=${combinationCode}`).then(Combination => {
      console.log(Combination)
      commit(types.COMBINATION_QUERY_SUC, {Combination});
    })
    // return http.post(`portfolio/query/get_mapping`, obj).then(data => {
    //   var Combination = data.result
    //   commit(types.COMBINATION_QUERY_SUC, {Combination});
    // })
  },

  querySysPortfoliosCount({commit}, {pageIndex = 1, pageSize = 10} = {}) {
    commit(types.PORTFOLIOS_QUERY_COUNT_BEGIN);

    return http.get(`portfolio/query?page=${pageIndex}&size=${pageSize}`)
      .then(data => {
        var totalCount = data.totalElements
        console.log('总条数')
        console.log(totalCount)
        commit(types.PORTFOLIOS_QUERY_COUNT_SUC, {totalCount});
      })
  },

  querySysPortfolios({commit}, {pageIndex = 1, pageSize = 10} = {}) {
    commit(types.PORTFOLIO_QUERY_PORTFOLIOS_BEGIN);
    return http.get(`portfolio/query?page=${pageIndex}&size=${pageSize}`).then(data => {
      console.log(data.content)
      var Portfolios = data.content
      for (var i = 0; i < Portfolios.length; i++) {
        if (Portfolios[i].state === 0) {
          Portfolios[i].state = '下架'
        } else if (Portfolios[i].state === 1) {
          Portfolios[i].state = '上架'
        }
        if (Portfolios[i].runDay === '') {
          Portfolios[i].runDay = '0'
        } else if (!Portfolios[i].runDay) {
          Portfolios[i].runDay = '0'
        }
      }
      commit(types.PORTFOLIO_QUERY_PORTFOLIOS_SUC, {Portfolios});
    })
  },

  saveSysPortfolio({commit}, Portfolio) {
    console.log(Portfolio)
    var timestring = parseInt(Date.parse(new Date(Portfolio.createDate))) / 1000
    Portfolio.createDate = timestring
    var dataobj = {}
    dataobj.combinationCode = Portfolio.combinationCode
    dataobj.combinationName = Portfolio.combinationName
    dataobj.briefIntroduction = Portfolio.briefIntroduction
    dataobj.description = Portfolio.description
    dataobj.limitMoneyPurchased = Portfolio.limitMoneyPurchased
    dataobj.baseAnnualizedIncome = Portfolio.baseAnnualizedIncome
    dataobj.description = Portfolio.description
    dataobj.state = Portfolio.state
    dataobj.baseCalculate = Portfolio.baseCalculate
    //dataobj.createDate=Portfolio.createDate
    // return http.post(`portfolio/query?combinationCode=${Portfolio.combinationCode}&combinationName=${bulletin.combinationName}&briefIntroduction=${bulletin.briefIntroduction}&description=${bulletin.description}&limitMoneyPurchased=${bulletin.limitMoneyPurchased}&baseAnnualizedIncome=${bulletin.baseAnnualizedIncome}&createDate=${timestring}`).then(bulletin => {
    //   commit(types.PORTFOLIO_ADD_SUC, {Portfolio})
    // });
    return http.post(`portfolio/query`, dataobj).then(Portfolio => {
      commit(types.PORTFOLIO_ADD_SUC, {Portfolio}
      )
    })
      ;
  },

  updateSysPortfolio({commit}, Portfolio) {
    // return http.post(`portfolio/query/update?combinationCode=${Portfolio.combinationCode}&combinationName=${bulletin.combinationName}&briefIntroduction=${bulletin.briefIntroduction}&description=${bulletin.description}&limitMoneyPurchased=${bulletin.limitMoneyPurchased}&baseAnnualizedIncome=${bulletin.baseAnnualizedIncome}&createDate=${bulletin.createDate}&combinationId=${bulletin.combinationId}&state=${bulletin.state}`).then(data => {
    //   commit(types.PORTFOLIO_UPDATE_SUC, {Portfolio})
    // });
    var dataobj = {}
    dataobj.combinationId = Portfolio.combinationId
    dataobj.baseCalculate = Portfolio.baseCalculate
    dataobj.briefIntroduction = Portfolio.briefIntroduction
    dataobj.description = Portfolio.description
    dataobj.limitMoneyPurchased = Portfolio.limitMoneyPurchased
    dataobj.accumulatedIncome = Portfolio.accumulatedIncome
    dataobj.annualizedIncome = Portfolio.annualizedIncome
    dataobj.dailyCent = Portfolio.dailyCent
    dataobj.runDay = Portfolio.runDay
    console.log(Portfolio.state)
    dataobj.state = Portfolio.state
    if (Portfolio.state === '上架') {
      dataobj.state = 1
    } else if (Portfolio.state === '上架') {
      dataobj.state = 2
    }
    //dataobj.baseAnnualizedIncome = Portfolio.baseAnnualizedIncome
    return http.post(`portfolio/query/update`, dataobj).then(data => {
      commit(types.PORTFOLIO_UPDATE_SUC, {Portfolio}
      )
    })
      ;
  },

  saveSysCombination({commit}, Combination) {
    console.log(Combination)

    var dataobj = {}
    dataobj.fundCombinationCode = Combination.fundCombinationCode
    //dataobj.fundCombinationCode = Combination.fundCombinationCode
    dataobj.fundCode = Combination.fundCode
    dataobj.fundPercent = Combination.fundPercent
    dataobj.fundShareType = Combination.fundShareType
    //dataobj.createDate=Portfolio.createDate
    // return http.post(`portfolio/query?combinationCode=${Portfolio.combinationCode}&combinationName=${bulletin.combinationName}&briefIntroduction=${bulletin.briefIntroduction}&description=${bulletin.description}&limitMoneyPurchased=${bulletin.limitMoneyPurchased}&baseAnnualizedIncome=${bulletin.baseAnnualizedIncome}&createDate=${timestring}`).then(bulletin => {
    //   commit(types.PORTFOLIO_ADD_SUC, {Portfolio})
    // });
    return http.post(`portfolio/query/add_mapping`, dataobj).then(Combination => {
      commit(types.COMBINATION_ADD_SUC, {Combination}
      )
    })
      ;
  },

  deleteSysCombination({commit}, {mappingId} = {}) {
    return http.post(`portfolio/query/delete_mapping?mappingId=${mappingId}`).then(data => {
      console.log(data)
    })
      ;
  },

  deleteSysPortfolio({commit}, {combinationCode}) {
    return http.get(`portfolio/query/delete?combinationCode=${combinationCode}`).then(data => {
      commit(types.PORTFOLIO_DEL_SUC, {combinationCode}
      )
    })
      ;
  },

  editfiles({commit}, {mappingId}) {
    return http.get(`portfolio/query/query_mapping?mappingId=${mappingId}`).then(data => {
      return data
    })
      ;
  },

  editFund({commit}, {mappingId, fundPercent}) {
    return http.post(`portfolio/query/update_mapping?mappingId=${mappingId}&fundPercent=${fundPercent}`).then(data => {


    })
      ;
  }
}


export default actions;
